<?php

/**
 * Created by PhpStorm.
 * User: zuoa
 * Date: 2016/11/21 0021
 * Time: 09:56
 */
class User_Model extends CI_Model
{
    private $table_name = "tbl_police";

    public $userdata = null;

    public function __construct()
    {
        $this->load->database();
    }

    private function is_admin()
    {
        return $this->userdata != null && (((int)$this->userdata["is_admin"]) == 1);
    }

    public function upsert($u)
    {
        $this->db->trans_start();
        $user = $this->get_user($u["openid"]);
        if (is_null($user)) {
            $this->db->insert($this->table_name, $u);
        } else {
            $this->db->where('openid', $u["openid"])->update($this->table_name, $u);
        }
        $this->db->trans_complete();
    }


    public function get_users()
    {
        $query = $this->db->get($this->table_name);
        return $query->result_array();
    }

    public function get_uncheck_count()
    {
        if ($this->is_admin()) {
            $query = $this->db->where("checked", 0)->get($this->table_name);
            return $query->num_rows();
        } else {
            $query = $this->db->where(array("checked" => 0, "department" => $this->userdata["userdept"]))->get($this->table_name);
            return $query->num_rows();
        }
    }

    public function get_checked_users($search = '')
    {
        if ($this->is_admin()) {
            if ($search == '') {
                $query = $this->db->where("checked", 1)->order_by("create_time", "desc")->get($this->table_name);
                return $query->result_array();
            } else {
                $query = $this->db->where("checked", 1)->like("name", $search)->order_by("create_time", "desc")->get($this->table_name);
                return $query->result_array();
            }
        } else {
            if ($search == '') {
                $query = $this->db->where(array("checked" => 1, "department" => $this->userdata["userdept"]))->order_by("create_time", "desc")->get($this->table_name);
                return $query->result_array();
            } else {
                $query = $this->db->where(array("checked" => 1, "department" => $this->userdata["userdept"]))->like("name", $search)->order_by("create_time", "desc")->get($this->table_name);
                return $query->result_array();
            }
        }

    }

    public function get_uncheck_users($search = '')
    {
        if ($this->is_admin()) {
            if ($search == '') {
                $query = $this->db->where("checked", 0)->order_by("create_time", "desc")->get($this->table_name);
                return $query->result_array();
            } else {
                $query = $this->db->where("checked", 0)->like("name", $search)->order_by("create_time", "desc")->get($this->table_name);
                return $query->result_array();
            }
        } else {
            if ($search == '') {
                $query = $this->db->where(array("checked" => 0, "department" => $this->userdata["userdept"]))->order_by("create_time", "desc")->get($this->table_name);
                return $query->result_array();
            } else {
                $query = $this->db->where(array("checked" => 0, "department" => $this->userdata["userdept"]))->like("name", $search)->order_by("create_time", "desc")->get($this->table_name);
                return $query->result_array();
            }
        }
    }

    public function add_preset_police($u)
    {
        return $this->db->insert("tbl_preset_police", $u);
    }

    public function get_preset_users($search = '')
    {
        if ($this->is_admin()) {
            if ($search == '') {
                $query = $this->db->get("tbl_preset_police");
                return $query->result_array();
            } else {
                $query = $this->db->like("name", $search)->get("tbl_preset_police");
                return $query->result_array();
            }
        } else {
            if ($search == '') {
                $query = $this->db->where("department", $this->userdata["userdept"])->get("tbl_preset_police");
                return $query->result_array();
            } else {
                $query = $this->db->where("department" , $this->userdata["userdept"])->like("name", $search)->get("tbl_preset_police");
                return $query->result_array();
            }
        }
    }

    public function get_user($id)
    {
        $query = $this->db->where("openid", $id)->get($this->table_name);
        return $query->first_row();
    }

    public function check_user($id, $check)
    {
        $data = array(
            'checked' => $check
        );

        $this->db->update($this->table_name, $data, array('openid' => $id));
    }

    public function get_usage_statistics()
    {
        $sql = "SELECT department, COUNT(*) as count FROM `tbl_police` WHERE checked > 0 GROUP BY department ORDER BY count DESC LIMIT 10;";
        $query = $this->db->query($sql);
        return $query->result_array();
    }
}